/*
 *****************************************************************************
 * Copyright (C) 2005-2014 UC Mobile Limited. All Rights Reserved
 * File			: StockViewController.mm
 *
 * Description	: StockViewController
 *
 * Author		: lixm@ucweb.com
 *
 * History		: Creation, 15/5/17, lixm@ucweb.com, Create the file
 ******************************************************************************
 **/

#import "StockViewController.h"

#define SINA_STOCK_API @"http://hq.sinajs.cn/list=sh601006"

@interface StockViewController ()<NSURLConnectionDelegate>
{
    NSMutableData *m_Data;
}

@end

@implementation StockViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    //Test
    [self requestStockData];
}

-(void)requestStockData
{
    m_Data = nil;
    NSURLRequest *stockRequest  = [NSURLRequest requestWithURL:[NSURL URLWithString:SINA_STOCK_API]];
    [NSURLConnection connectionWithRequest:stockRequest delegate:self];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)connection:(NSURLConnection *)theConnection didReceiveResponse:(NSURLResponse *)response
{
    
    NSInteger responseCode = [(NSHTTPURLResponse *)response statusCode];
    NSLog(@"response length=%lld  statecode=%ld", [response expectedContentLength],responseCode);
}




// A delegate method called by the NSURLConnection as data arrives.  The
// response data for a POST is only for useful for debugging purposes,
// so we just drop it on the floor.
- (void)connection:(NSURLConnection *)theConnection didReceiveData:(NSData *)data
{
    if (m_Data == nil) {
        m_Data = [[NSMutableData alloc] initWithData:data];
    } else {
        [m_Data appendData:data];
    }
    NSLog(@"response connection %@",m_Data);
}

// A delegate method called by the NSURLConnection if the connection fails.
// We shut down the connection and display the failure.  Production quality code
// would either display or log the actual error.
- (void)connection:(NSURLConnection *)theConnection didFailWithError:(NSError *)error
{
    
    NSLog(@"response error%@", [error localizedFailureReason]);
}

// A delegate method called by the NSURLConnection when the connection has been
// done successfully.  We shut down the connection with a nil status, which
// causes the image to be displayed.
- (void)connectionDidFinishLoading:(NSURLConnection *)theConnection
{
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding (kCFStringEncodingGB_18030_2000);
    NSString *responseString = [[NSString alloc] initWithData:m_Data encoding:enc];
    NSLog(@"response body%@", responseString);
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
